<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职称" prop="expertTitle">
        <el-input
          v-model="queryParams.expertTitle"
          placeholder="请输入职称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入职位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="研究方向" prop="researchField">
        <el-input
          v-model="queryParams.researchField"
          placeholder="请输入研究方向"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作年限" prop="workYears">
        <el-input
          v-model="queryParams.workYears"
          placeholder="请输入工作年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['expertInfo:expertInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['expertInfo:expertInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['expertInfo:expertInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['expertInfo:expertInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expertInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专家ID" align="center" prop="expertId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="专家页面首图" align="center" prop="expertImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.expertImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="专家介绍" align="center" prop="expertIntroduce" />
      <el-table-column label="职称" align="center" prop="expertTitle" />
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="研究方向" align="center" prop="researchField" />
      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column label="工作年限" align="center" prop="workYears" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['expertInfo:expertInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['expertInfo:expertInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="专家页面首图" prop="expertImage">
          <image-upload v-model="form.expertImage"/>
        </el-form-item>
        <el-form-item label="专家介绍" prop="expertIntroduce">
          <el-input v-model="form.expertIntroduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="职称" prop="expertTitle">
          <el-input v-model="form.expertTitle" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="研究方向" prop="researchField">
          <el-input v-model="form.researchField" placeholder="请输入研究方向" />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="工作年限" prop="workYears">
          <el-input v-model="form.workYears" placeholder="请输入工作年限" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">专家证书信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddExpertCertificates">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteExpertCertificates">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="expertCertificatesList" :row-class-name="rowExpertCertificatesIndex" @selection-change="handleExpertCertificatesSelectionChange" ref="expertCertificates">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="显示顺序" prop="orderNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderNum" placeholder="请输入显示顺序" />
            </template>
          </el-table-column>
          <el-table-column label="证书的编号，具有唯一性" prop="certificateNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.certificateNumber" placeholder="请输入证书的编号，具有唯一性" />
            </template>
          </el-table-column>
          <el-table-column label="证书的具体名称" prop="certificateName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.certificateName" placeholder="请输入证书的具体名称" />
            </template>
          </el-table-column>
          <el-table-column label="颁发该证书的机构名称" prop="issuingOrganization" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.issuingOrganization" placeholder="请输入颁发该证书的机构名称" />
            </template>
          </el-table-column>
          <el-table-column label="证书的颁发日期" prop="issueDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.issueDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择证书的颁发日期" />
            </template>
          </el-table-column>
          <el-table-column label="证书有效期的起始日期" prop="validityStartDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.validityStartDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择证书有效期的起始日期" />
            </template>
          </el-table-column>
          <el-table-column label="证书有效期的结束日期，可为空表示长期有效" prop="validityEndDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.validityEndDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择证书有效期的结束日期，可为空表示长期有效" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
<!--          <div class="el-upload__tip" slot="tip">-->
<!--            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
<!--          </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExpertInfo, getExpertInfo, delExpertInfo, addExpertInfo, updateExpertInfo } from "@/api/expertInfo/expertInfo";
import { getToken } from "@/utils/auth";
export default {
  name: "ExpertInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/expertInfo/expertInfo/importData"
      },
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedExpertCertificates: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家信息表格数据
      expertInfoList: [],
      // 专家证书表格数据
      expertCertificatesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        expertImage: null,
        expertIntroduce: null,
        expertTitle: null,
        position: null,
        researchField: null,
        education: null,
        workYears: null,
        orderNum: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专家信息列表 */
    getList() {
      this.loading = true;
      listExpertInfo(this.queryParams).then(response => {
        this.expertInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('expertInfo/expertInfo/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 表单重置
    reset() {
      this.form = {
        expertId: null,
        userId: null,
        expertImage: null,
        expertIntroduce: null,
        expertTitle: null,
        position: null,
        researchField: null,
        education: null,
        workYears: null,
        orderNum: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.expertCertificatesList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.expertId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专家信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const expertId = row.expertId || this.ids
      getExpertInfo(expertId).then(response => {
        this.form = response.data;
        this.expertCertificatesList = response.data.expertCertificatesList;
        this.open = true;
        this.title = "修改专家信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.expertCertificatesList = this.expertCertificatesList;
          if (this.form.expertId != null) {
            updateExpertInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpertInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const expertIds = row.expertId || this.ids;
      this.$modal.confirm('是否确认删除专家信息编号为"' + expertIds + '"的数据项？').then(function() {
        return delExpertInfo(expertIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 专家证书序号 */
    rowExpertCertificatesIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 专家证书添加按钮操作 */
    handleAddExpertCertificates() {
      let obj = {};
      obj.orderNum = "";
      obj.certificateNumber = "";
      obj.certificateName = "";
      obj.issuingOrganization = "";
      obj.issueDate = "";
      obj.validityStartDate = "";
      obj.validityEndDate = "";
      this.expertCertificatesList.push(obj);
    },
    /** 专家证书删除按钮操作 */
    handleDeleteExpertCertificates() {
      if (this.checkedExpertCertificates.length == 0) {
        this.$modal.msgError("请先选择要删除的专家证书数据");
      } else {
        const expertCertificatesList = this.expertCertificatesList;
        const checkedExpertCertificates = this.checkedExpertCertificates;
        this.expertCertificatesList = expertCertificatesList.filter(function(item) {
          return checkedExpertCertificates.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleExpertCertificatesSelectionChange(selection) {
      this.checkedExpertCertificates = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('expertInfo/expertInfo/export', {
        ...this.queryParams
      }, `expertInfo_${new Date().getTime()}.xlsx`)
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
